package com.base.cn.platform.os.service.statistics.goods;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.order.order.OrderDetailsDao;
import com.base.cn.platform.os.dao.statistics.goods.ZStatisticsGoodsUVCountDao;
import com.base.cn.platform.os.entity.order.order.OrderDetails;
import com.base.cn.platform.os.entity.order.shop.ShopData;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.goods.GoodsPayRankingCount;
import com.base.cn.platform.os.entity.statistics.goods.ZStatisticsGoodsUVCount;
import com.base.cn.platform.os.service.order.init.ShopDataInit;
import com.base.cn.platform.os.service.order.order.OrderDetailsBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品UV统计Biz
 *
 * @author s.li
 * @date 2019/04/24
 */
@Service
public class ZStatisticsGoodsUVCountBiz extends BaseBiz<ZStatisticsGoodsUVCount, ZStatisticsGoodsUVCountDao> {
    private static final String TIME_FORMAT = "yyyy-MM-dd";
    @Autowired
    private ZStatisticsGoodsUVCountDao zStatisticsGoodsUVCountDao;
    @Autowired
    private ShopDataInit shopDataInit;
    @Autowired
    private OrderDetailsBiz orderDetailsBiz;
    @Autowired
    private OrderDetailsDao orderDetailsDao;

    /**
     * 生成商品UV统计记录
     * @param goodsUVCountList
     */
    @Transactional
    public synchronized void genGoodsUVCountList(List<ZStatisticsGoodsUVCount> goodsUVCountList){
        Date nowDate = new Date();
        //获取列表中的时间条件
        String timeWhereSql = goodsUVCountList.stream().map(e-> "countTime ='"+ DateUtils.format(e.getCountTime(), TIME_FORMAT)+"'").collect(Collectors.joining(" or "," 1=1 and (",")"));
        List<ZStatisticsGoodsUVCount> uvCountList = this.find(timeWhereSql,null,null);
        List<ZStatisticsGoodsUVCount> updateList = new ArrayList<>();
        List<ZStatisticsGoodsUVCount> addList = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(uvCountList)){
            Map<String,ZStatisticsGoodsUVCount> oldUVMap = uvCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(), TIME_FORMAT)+"_"+e.getGoodsType()+"_"+e.getGoodsId(), e->e));
            Map<String,ZStatisticsGoodsUVCount> newUVMap = goodsUVCountList.stream().collect(Collectors.toMap(e->DateUtils.format(e.getCountTime(), TIME_FORMAT)+"_"+e.getGoodsType()+"_"+e.getGoodsId(), e->e));
            newUVMap.forEach((k,v)->{
                ZStatisticsGoodsUVCount oldData = oldUVMap.get(k);
                if(ObjectUtils.isNotEmpty(oldData)){
                    oldData.setUvCount(v.getUvCount());
                    oldData.setUpdateTime(nowDate);
                    updateList.add(oldData);
                }else{
                    addList.add(v);
                }
            });
        }else{
            addList.addAll(goodsUVCountList);
        }
        if(ObjectUtils.isNotEmpty(addList)){
            this.batchSave(addList);
        }
        if(ObjectUtils.isNotEmpty(updateList)){
            this.updateBatch(updateList);
        }
    }

    /**
     * 访客榜 TOP5 之 商品
     * @param dateQueryCondition
     * @return List<ZStatisticsGoodsUVCount>
     */
    public List<ZStatisticsGoodsUVCount> queryUVTopFiveGoodsList(StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,TIME_FORMAT);
        //查询商品的数据
        List<ZStatisticsGoodsUVCount> goodsUVCountList = zStatisticsGoodsUVCountDao.queryUVTopFiveGoodsList(ZStatisticsGoodsUVCount.class,timeSql);
        if(ObjectUtils.isNotEmpty(goodsUVCountList)){
            //设置商品数据内容
            List<ShopData> shopDataList = new ArrayList<>();
            goodsUVCountList.forEach(e->{
                ShopData shop = new ShopData();
                shop.setShopId(e.getGoodsId());
                shop.setShopType(e.getGoodsType());
                shopDataList.add(shop);
            });
            shopDataInit.setShopData(shopDataList);
            Map<String,ShopData> shopMap = shopDataList.stream().collect(Collectors.toMap(e->e.getShopType()+"_"+e.getShopId(),e->e));
            goodsUVCountList.forEach(e->{
                ShopData shopData = shopMap.get(e.getGoodsType()+"_"+e.getGoodsId());
                e.setShop(shopData);
            });
            String shopSql = goodsUVCountList.stream().map(e->"(shopType='"+e.getGoodsType()+"' and shopId="+e.getGoodsId()+")").collect(Collectors.joining(" or ","(",")"));
            //设置商品的下单量
            Map<String,String> params = new HashMap<>();
            params.put("timeSql",timeSql);
            params.put("shopSql",shopSql);
            //查询商品的下单量
            List<Map<String,Object>> orderNumList = orderDetailsBiz.queryShopOrderNumList(params);
            if(ObjectUtils.isNotEmpty(orderNumList)){
                Map<String,Object> orderNumMap = orderNumList.stream().collect(Collectors.toMap(e->e.get("shopKey").toString(),e->e.get("orderNum")));
                goodsUVCountList.forEach(e->{
                    String shopKey = e.getGoodsType()+"_"+e.getGoodsId();
                    Long orderNum = (Long) orderNumMap.get(shopKey);
                    if(orderNum==null){
                        orderNum = 0L;
                    }
                    e.setOrderNum(orderNum.intValue());
                });
            }
        }
        return goodsUVCountList;
    }

    /**
     * 查询支付榜 TOP5 之 付费商品
     * @return List<Map<String,Object>>
     */
    public List<GoodsPayRankingCount> queryGoodsPayTopFiveList(StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,TIME_FORMAT);
        List<GoodsPayRankingCount> goodsPayRankingCountList = orderDetailsDao.queryGoodsPayTopFiveList(OrderDetails.class,timeSql);
        if(ObjectUtils.isNotEmpty(goodsPayRankingCountList)){
            List<ShopData> shopDataList = new ArrayList<>();
            goodsPayRankingCountList.forEach(e->{
                ShopData shopData = new ShopData();
                String[] arr = e.getShopKey().split("_");
                shopData.setShopType(arr[0]);
                shopData.setShopId(new BigDecimal(arr[1]));
                shopDataList.add(shopData);
            });
            shopDataInit.setShopData(shopDataList);
            Map<String,ShopData> shopMap = shopDataList.stream().collect(Collectors.toMap(e->e.getShopType()+"_"+e.getShopId(),e->e));
            goodsPayRankingCountList.forEach(e->{
                e.setShop(shopMap.get(e.getShopKey()));
            });
        }
        return goodsPayRankingCountList;
    }

    /**
     * 访客榜 TOP5 之 商品
     * @param params
     * @return List<ZStatisticsGoodsUVCount>
     */
    public List<ZStatisticsGoodsUVCount> queryGoodsUVCountList(Map<String,String> params){
        return zStatisticsGoodsUVCountDao.queryGoodsUVCountList(ZStatisticsGoodsUVCount.class,params);
    }

    /**
     * 查询商品UV总量
     * @param timeSql
     * @return List<ZStatisticsGoodsUVCount>
     */
    public List<ZStatisticsGoodsUVCount> queryGoodsSumUVCountList(String timeSql){
        return zStatisticsGoodsUVCountDao.queryGoodsSumUVCountList(ZStatisticsGoodsUVCount.class,timeSql);
    }
}
